package org.springcloud.wisdom.spring_01_canteen.loginmsg.service;

import org.springcloud.wisdom.spring_01_canteen.common.bean.Page;
import org.springcloud.wisdom.spring_01_canteen.login.entity.Teacher;
import org.springcloud.wisdom.spring_01_canteen.loginmsg.dao.AdminTeacherDao;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AdminTeacherService {
    @Autowired
    AdminTeacherDao adminTeacherDao;

    //获取所有老师的详细信息
    public Page<Teacher> getPage(int pageNum, int pageSize, String tname, Integer dept, String tid){
        //计算起始位置
        int start=(pageNum-1)*pageSize;
        //获取分页的数据
        List<Teacher> list=adminTeacherDao.getByCond(start,pageSize,tname,dept,tid);
        //获取总条目数
        int total=adminTeacherDao.countTeacher(tname,dept,tid);
        //计算总页数
        int pages =(total % pageSize == 0 ? total / pageSize : total / pageSize + 1);
        //创建分页对象
        Page<Teacher> page=new Page<>();
        page.setRecords(list);
        page.setTotal(total);
        page.setPages(pages);
        page.setCurrent(pageNum);
        page.setSize(pageSize);
        return page;
    }

    //删除某个教师
    public  boolean delT(String tid){
        return adminTeacherDao.delTeacher(tid)>0;
    }

    //修改老师信息
    public boolean updTeach(Teacher teacher){
        if (teacher ==null){
            return false;
        }
        return adminTeacherDao.updateTeacher(teacher)>0;
    }

    //添加某个老师的信息
    public boolean addTeach(Teacher teacher){
        if (teacher == null){
            return  false;
        }
        return adminTeacherDao.addTeacher(teacher) > 0;
    }

}
